import { useSelector, useDispatch } from "react-redux"
import { useCallback } from "react";
import TotalTypeApi from "../api/totalType";
import Creater from "../store/actions"

//子类hoock
export const useChilType = () => {

  const dispatch = useDispatch();
  const childType = useSelector(state => state.childType)


  const { curPage,
    eachPage,
    total,
    maxPage,
    TatolData,
    rows } = childType
  // 获取商品数据
  const getChilType = useCallback(async () => {
    // 调用接口获取数据
    const data = await TotalTypeApi.getChildType({
      curPage,
      eachPage
    });
    ;
    // 将通过接口获取到的数据合并到总数据中去
    dispatch(Creater.getChildType({ data }));
  }, [eachPage, curPage]);

  // 新增子类

  const addchild = async (info) => {

    const result = await TotalTypeApi.addchilddata(info)
    const data = await TotalTypeApi.getChildType({
      curPage,
      eachPage
    });
    ;
    // 将通过接口获取到的数据合并到总数据中去
    dispatch(Creater.getChildType({ data }));

    return result


  }
  // c查询
  const selectfromTotalType = async (info) => {
    const data = {}
    data.rows = await TotalTypeApi.selectFromtotal(info)
    dispatch(Creater.getChildType({ data }));
    return data.rows
  }
  //删除
  const delchildOne = async (info) => {
    const result = await TotalTypeApi.deleteone(info)
    // 调用接口获取数据
    const data = await TotalTypeApi.getChildType({
      curPage,
      eachPage
    });
    ;
    // 将通过接口获取到的数据合并到总数据中去
    dispatch(Creater.getChildType({ data }));
    return result


  }
  // 修改
  const resOneChildType = async (info) => {
    const result = await TotalTypeApi.resOneChild(info)
    // 调用接口获取数据
    const data = await TotalTypeApi.getChildType({
      curPage,
      eachPage
    });
    ;
    // 将通过接口获取到的数据合并到总数据中去
    dispatch(Creater.getChildType({ data }));
    return result

  }


  return [{
    curPage,
    eachPage,
    total,
    maxPage, TatolData,
    rows
  }, { getChilType, addchild, selectfromTotalType, delchildOne, resOneChildType }]
}